﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Interfleet.SE.UICompiler.Core.ClosureCompiler;
using Interfleet.SE.UICompiler.Core;
using Moq;

namespace Interfleet.SE.UICompiler.Tests.ClosureCompiler
{
    /// <summary>
    /// Summary description for CCResultBuilderTests
    /// </summary>
    [TestClass]
    public class ResultBuilderTests
    {
        public ResultBuilderTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private CCArguments Arguments
        {
            get
            {
                var args = new CCArguments { CompiledFileName = "b" };
                args.Files.Add("a");
                return args;
            }
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CCResultBuilder_Constructor_When_ArgumentsIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new CCResultBuilder(null, new Mock<IFileProvider>().Object);
            Assert.Fail();
        }

        [TestMethod]
        public void CCResultBuilder_Build_When_StatusMessageIsEmpty_Then_SuccessIsTrue()
        {
            var mock = new Mock<IFileProvider>();
            mock.Setup(m => m.FileExists(It.IsAny<string>())).Returns(true);
            mock.Setup(m => m.GetLastWriteTime(It.IsAny<string>())).Returns(DateTime.Now.AddSeconds(-2));
            ResultBuilder builder = new CCResultBuilder(Arguments, mock.Object);
            var result = builder.Build(string.Empty, DateTime.Now.AddSeconds(-5));
            Assert.IsTrue(result.Success);
        }

        [TestMethod]
        public void ResultBuilder_Build_When_StatusMessageIsNotEmpty_Then_SuccessIsFalse()
        {
            ResultBuilder builder = new CCResultBuilder(Arguments, new Mock<IFileProvider>().Object);
            var result = builder.Build("a", DateTime.Now);
            Assert.IsFalse(result.Success);
        }

        [TestMethod]
        public void ResultBuilder_Build_When_FileCreatedTimeIsOlderThan8Seconds_Then_SuccessIsFalse()
        {
            var mock = new Mock<IFileProvider>();
            mock.Setup(m => m.FileExists(It.IsAny<string>())).Returns(true);
            mock.Setup(m => m.GetLastWriteTime(It.IsAny<string>())).Returns(DateTime.Now.AddSeconds(-9));
            var startTime = DateTime.Now.AddSeconds(-8);
            ResultBuilder builder = new CCResultBuilder(Arguments, mock.Object);
            var result = builder.Build(string.Empty, startTime);
            Assert.IsFalse(result.Success);
        }

        [TestMethod]
        public void ResultBuilder_Build_When_FileCreatedTimeIsOlderThan8Seconds_Then_StatusMessageContainsInfo()
        {
            var mock = new Mock<IFileProvider>();
            mock.Setup(m => m.FileExists(It.IsAny<string>())).Returns(true);
            mock.Setup(m => m.GetLastWriteTime(It.IsAny<string>())).Returns(DateTime.Now.AddSeconds(-9));
            var startTime = DateTime.Now.AddSeconds(-8);
            ResultBuilder builder = new CCResultBuilder(Arguments, mock.Object);
            var result = builder.Build(string.Empty, startTime);
            StringAssert.Contains(result.StatusMessage, "A new file was not created");
        }

        [TestMethod]
        public void ResultBuilder_Build_When_NoFileWasCreated_Then_SuccessIsFalse()
        {
            var mock = new Mock<IFileProvider>();
            mock.Setup(m => m.FileExists(It.IsAny<string>())).Returns(false);
            mock.Setup(m => m.GetLastWriteTime(It.IsAny<string>())).Returns(DateTime.Now.AddSeconds(-9));
            var startTime = DateTime.Now.AddSeconds(-10);
            ResultBuilder builder = new CCResultBuilder(Arguments, mock.Object);
            var result = builder.Build(string.Empty, startTime);
            Assert.IsFalse(result.Success);
        }

        [TestMethod]
        public void ResultBuilder_Build_When_FileIsOlderThanStartTime_Then_SuccessIsFalse()
        {
            var mock = new Mock<IFileProvider>();
            mock.Setup(m => m.FileExists(It.IsAny<string>())).Returns(true);
            mock.Setup(m => m.GetLastWriteTime(It.IsAny<string>())).Returns(DateTime.Now.AddSeconds(-9));
            var startTime = DateTime.Now.AddSeconds(-5);
            ResultBuilder builder = new CCResultBuilder(Arguments, mock.Object);
            var result = builder.Build(string.Empty, startTime);
            Assert.IsFalse(result.Success);
        }
    }

}
